package edu.spsu.courses;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import edu.spsu.R;

public class CourseAdapter extends ArrayAdapter {
	private final String TAG = "CourseAdapter";
	private ArrayList<Course> items = new ArrayList<Course>();
	private Context context;

	public CourseAdapter(Context context, ArrayList<Course> items) {
		super(context, R.layout.course_row, items);
		this.context = context;
		this.items = items;
		Log.d(TAG,"items in CourseAdapter constructor is size:"+items.size());
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = null;
		Log.d(TAG, "Inside getVIEW!!");
		// if (convertView != null) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		rowView = inflater.inflate(R.layout.course_row, parent, false);
//		TextView numberTextView = (TextView) rowView.findViewById(R.id.number);
		TextView nameTextView = (TextView) rowView.findViewById(R.id.name);
		TextView titleTextView = (TextView) rowView.findViewById(R.id.title);
		TextView hoursTextView = (TextView) rowView.findViewById(R.id.hours);
		TextView daysTextView = (TextView) rowView.findViewById(R.id.days);
		TextView weeksTextView = (TextView) rowView.findViewById(R.id.weeks);
		TextView timeTextView = (TextView) rowView.findViewById(R.id.time);
		TextView instructorTextView = (TextView) rowView
				.findViewById(R.id.instructor);
		TextView locationTextView = (TextView) rowView
				.findViewById(R.id.location);
		TextView studentCountTextView = (TextView) rowView
				.findViewById(R.id.studentCount);
		// textView.setText("YEEEAH!");

		 Course thisCourse = null;
		if (items != null && items.size() > position) {
			thisCourse = (Course) items.get(position);
			LinearLayout overallLayout = (LinearLayout)rowView.findViewById(R.id.rowLayout);
			
			if(thisCourse.getName() == null || thisCourse.getName().equals("")) {
				LinearLayout nameLayout = (LinearLayout)rowView.findViewById(R.id.nameLayout);
				overallLayout.removeView(nameLayout);
			} else {
				nameTextView.setText(thisCourse.getName());
			}
			
			if(thisCourse.getTitle() == null || thisCourse.getTitle().equals("")) {
				LinearLayout titleLayout = (LinearLayout)rowView.findViewById(R.id.titleLayout);
				overallLayout.removeView(titleLayout);
			} else {
				titleTextView.setText(thisCourse.getTitle());
			}
			
			if(thisCourse.getHours() == null || thisCourse.getHours().equals("")) {
				LinearLayout hoursLayout = (LinearLayout)rowView.findViewById(R.id.hoursLayout);
				overallLayout.removeView(hoursLayout);
			} else {
				hoursTextView.setText(thisCourse.getHours());
			}
			
			if(thisCourse.getDays() == null || thisCourse.getDays().equals("")) {
				LinearLayout daysLayout = (LinearLayout)rowView.findViewById(R.id.daysLayout);
				overallLayout.removeView(daysLayout);
			} else {
				daysTextView.setText(thisCourse.getDays());
			}
			
			if(thisCourse.getWeeks() == null || thisCourse.getWeeks().equals("")) {
				LinearLayout weeksLayout = (LinearLayout)rowView.findViewById(R.id.weeksLayout);
				overallLayout.removeView(weeksLayout);
			} else {
				weeksTextView.setText(thisCourse.getWeeks());
			}
			
			if(thisCourse.getTime() == null || thisCourse.getTime().equals("")) {
				LinearLayout timeLayout = (LinearLayout)rowView.findViewById(R.id.timeLayout);
				overallLayout.removeView(timeLayout);
			} else {
				timeTextView.setText(thisCourse.getTime());
			}
			
			if(thisCourse.getInstructor() == null || thisCourse.getInstructor().equals("")) {
				LinearLayout instructorLayout = (LinearLayout)rowView.findViewById(R.id.instructorLayout);
				overallLayout.removeView(instructorLayout);
			} else {
				instructorTextView.setText(thisCourse.getInstructor());
			}
			
			if(thisCourse.getLocation() == null || thisCourse.getLocation().equals("")) {
				LinearLayout locationLayout = (LinearLayout)rowView.findViewById(R.id.locationLayout);
				overallLayout.removeView(locationLayout);
			} else {
				locationTextView.setText(thisCourse.getLocation());
			}
			
			if(thisCourse.getStudentCount()==null || thisCourse.getStudentCount().equals("")) {
				LinearLayout studentCountLayout = (LinearLayout)rowView.findViewById(R.id.studentCountLayout);
				overallLayout.removeView(studentCountLayout);
			} else {
				studentCountTextView.setText(thisCourse.getStudentCount());		
			}
		}
		if (thisCourse == null) {
			Log.d(TAG, "NOOOOOOOOOOOOOOOOO! Position="+position+" items is size:"+items.size());
		} else {
			Log.d(TAG, "name=" + thisCourse.getName());
		}
		return rowView;
	}
	
	 public boolean areAllItemsEnabled() {
	        return false;
	    }
	 public boolean isEnabled(int position) {
	     return false;  // return false if position == positionYouWantToDisable
	    }

}
